package me.zhengjie.modules.finace_taxation.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.finace_taxation.domain.FtLeaveMessage;
import me.zhengjie.modules.finace_taxation.service.FtLeaveMessageService;
import me.zhengjie.modules.finace_taxation.service.dto.FtLeaveMessageDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtLeaveMessageQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "咨询留言和预定服务管理")
@RequestMapping("/api/ft/ftLeaveMessage")
public class FtLeaveMessageController {

    private final FtLeaveMessageService ftLeaveMessageService;

    @GetMapping
    @Log("查询留言咨询")
    @ApiOperation("查询留言咨询")
    public ResponseEntity query(@RequestParam Map<String,Object> map, Pageable pageable){
        return new ResponseEntity<>(ftLeaveMessageService.queryAll(map,pageable),HttpStatus.OK);
    }

    @GetMapping("/getScheduledService")
    @Log("查询预定服务列表")
    @ApiOperation("查询预定服务列表")
    public ResponseEntity getScheduledService(@RequestParam Map<String,Object> map, Pageable pageable){
        return new ResponseEntity<>(ftLeaveMessageService.getScheduledService(map,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增服务留言")
    @ApiOperation("新增服务留言")
    public ResponseEntity create(@Validated @RequestBody FtLeaveMessageDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftLeaveMessageService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改服务留言")
    @ApiOperation("修改服务留言")
    public ResponseEntity update(@Validated @RequestBody FtLeaveMessageDto resources){
        ftLeaveMessageService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除服务留言")
    @ApiOperation("删除服务留言")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftLeaveMessageService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出预定服务列表")
    @ApiOperation("导出预定服务列表")
    @GetMapping(value = "/getScheduledService/download")
    public void download(HttpServletResponse response, FtLeaveMessageQueryParam query) throws IOException {
        query.setType(2);
        ftLeaveMessageService.download(ftLeaveMessageService.downLoadData(query), response);
    }

    @Log("导出服务留言咨询")
    @ApiOperation("导出服务留言咨询")
    @GetMapping(value = "/download")
    public void downloadLeaveMessage(HttpServletResponse response, FtLeaveMessageQueryParam query) throws IOException {
        query.setType(1);
        ftLeaveMessageService.downloadLeaveMessage(ftLeaveMessageService.downLoadData(query), response);
    }

}
